<template>
  <div class="video-page" @click="handleClick">
    <van-nav-bar
      :fixed="true"
      :safe-area-inset-top="true"
      :placeholder="true"
      :border="false"
      @click-left="onClickLeft"
    >
      <template #left>
        <van-icon
          size="18"
          style="font-weight: bold"
          color="#fff"
          name="cross"
        />
      </template>
    </van-nav-bar>
    <!-- 加载状态 -->
    <div v-if="isLoading" class="loading-container">
      <van-loading type="spinner" color="#fff" size="40px" />
    </div>

    <!-- 错误提示 -->
    <div v-if="isError" class="error-container">
      <p>视频加载失败，请稍后再试。</p>
      <button @click="onClickLeft">确定</button>
    </div>

    <!-- 视频播放 -->
    <video
      loop
      ref="videoPlayer"
      :src="videoSrc"
      :autoplay="true"
      :controls="false"
      @canplay="onCanPlay"
      @error="onError"
    ></video>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from "vue";

// 视频路径
const videoSrc =
  new URL("@/assets/Service-img/video.mp4", import.meta.url).href +
  `?t=${Date.now()}`;
// 状态管理
const isLoading = ref(true); // 是否正在加载
const isError = ref(false); // 是否加载失败
const videoPlayer = ref(null); // 视频引用

// 超时计时器
let timeoutTimer = null;

// 处理点击事件 - 播放/暂停
const handleClick = () => {
  if (videoPlayer.value) {
    if (videoPlayer.value.paused) {
      videoPlayer.value.play();
    } else {
      videoPlayer.value.pause();
    }
  }
};

// 视频加载成功
const onCanPlay = () => {
  console.log("视频加载成功");
  clearTimeout(timeoutTimer); // 清除超时计时器
  isLoading.value = false; // 隐藏加载动画
};

// 视频加载失败
const onError = () => {
  console.log("视频加载失败");
  clearTimeout(timeoutTimer); // 清除超时计时器
  isLoading.value = false; // 隐藏加载动画
  isError.value = true; // 显示错误提示
};

// 页面加载时设置超时计时器
onMounted(() => {
  isLoading.value = true; // 显示加载动画
  timeoutTimer = setTimeout(() => {
    if (isLoading.value) {
      console.log("视频加载超时");
      isLoading.value = false; // 隐藏加载动画
      isError.value = true; // 显示错误提示
    }
  }, 10000); // 10秒超时
});

const onClickLeft = () => history.back();

// 清除计时器
onUnmounted(() => {
  clearTimeout(timeoutTimer);
});
</script>

<style lang="scss">
.video-page {
  position: relative;
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #000; /* 黑色背景 */
  overflow: hidden;
  .van-nav-bar {
    background: transparent;
  }

  video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover; /* 让视频铺满屏幕 */
    cursor: pointer;
  }

  .loading-container {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 999;
  }

  .error-container {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 999;
    color: #fff;
    font-size: 20px;
    text-align: center;

    p {
      margin-bottom: 10px;
    }

    button {
      padding: 10px 20px;
      background-color: #fff;
      color: #000;
      border: none;
      border-radius: 5px;
      font-size: 16px;
      cursor: pointer;

      &:hover {
        background-color: #ddd;
      }
    }
  }
}
</style>